package com.mdxq.mall.controller;

import com.mdxq.mall.response.R;
import com.mdxq.mall.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/minio")
@CrossOrigin("*")
public class MinioController {
    @Autowired
    private MinioService service;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.bucketName}")
    private String bucketName;

    @PostMapping("upload")
    public R upload(@RequestParam MultipartFile file){
        try {
            String filename=service.uploadFile(file);
            Map map=new HashMap<>();
            map.put("url",endpoint+"/"+bucketName+"/"+filename);
            return R.ok(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();
    }
}
